; Libs for assemble routines. Because I don't know how
; to use assemble in C.

global	lgdt_a		; void lgdt_a(t_32)
global	sgdt_a		; void sgdt_a(t_32)
global	lidt_a		; void lidt_a(t_32);

global	cli_a		; void cli_a();
global	sti_a		; void sti_a();

global	ltr_a		; void ltr_a(t_16);
global	lldt_a		; void lldt_a(t_16);

global	out_a		; void out_a(t_32 port, t_8 value);
global	in_a		; t_8 in_a(t_32 port);

global	hlt_a		; void hlt_a(void)

global port_write	; void port_write(char *buf, t_32 size);
global port_read	; void port_read(char *buf, t_32 size);

global	set_char
global	set_cursor
global	roll_back


lgdt_a:
	mov	eax, [esp+4]
	lgdt	[eax]
	ret

sgdt_a:
	mov	eax, [esp+4]
	sgdt	[eax]
	ret

lidt_a:
	mov	eax, [esp+4]
	lidt	[eax]
	ret

cli_a:
	cli
	ret

sti_a:
	sti
	ret

ltr_a:
	mov	eax, [esp + 4]
	ltr	ax
	ret

lldt_a:
	mov	eax, [esp + 4]
	lldt	ax
	ret

out_a:
	mov	edx, [esp + 4]
	mov	al, [esp + 8]
	out	dx, al
	nop
	nop
	ret

in_a:
	mov	edx, [esp + 4]
	xor	eax, eax
	in	al, dx
	nop
	nop
	ret

hlt_a:
	hlt

port_write:
	mov	esi, [esp + 4]
	mov	ecx, [esp + 8]
	mov	dx, 1F0h
	shr	ecx, 1
	rep	outsw
	ret

port_read:
	mov	edi, [esp + 4]
	mov	ecx, [esp + 8]
	mov	dx, 1F0h
	shr	ecx, 1
	rep	insw
	ret

set_char:
	mov	edi, [esp + 4]
	shl	edi, 1
	mov	eax, [esp + 8]
	mov	ah, 07h
	mov	[gs:edi], ax
	ret

DISP_ADDR	equ	03D4h
DISP_DATA	equ	03D5h
CURSOR_HIGH	equ	0Eh
CURSOR_LOW	equ	0Fh
set_cursor:
	mov	ebx, [esp + 4]
;	shl	ebx, 1
	mov	dx, DISP_ADDR
	mov	al, CURSOR_HIGH
	out	dx, al
	mov	dx, DISP_DATA
	mov	al, bh
	out	dx, al

	mov	edx, DISP_ADDR
	mov	al, CURSOR_LOW
	out	dx, al
	mov	edx, DISP_DATA
	mov	al, bl
	out	dx, al
	ret

roll_back:
	mov	dx, ds
	mov	bx, es

	mov	ax, gs
	mov	ds, ax
	mov	es, ax
	mov	edi, 0
	mov	esi, 160
	mov	ecx, 24 * 80
	rep	movsw

	mov	edi, 24 * 80 * 2
	mov	ecx, 80
	mov	ax, 0720h	; space
	rep	stosw

	mov	ds, dx
	mov	es, bx
	ret

global	memcpy	; char* memcpy(char *dest, char *sour, int bytes);
global	memset	; void *memset(void *buf, int c, int count);

memcpy:
	push	ebp
	mov	ebp, esp
	push	edi
	push	esi
	push	ecx

	mov	edi, [ebp + 8]
	mov	esi, [ebp + 12]
	mov	ecx, [ebp + 16]
	rep	movsb

	mov	eax, [ebp + 8]
	pop	ecx
	pop	esi
	pop	edi
	mov	esp, ebp
	pop	ebp
	ret

memset:
	push	ebp
	mov	ebp, esp
	push	edi
	push	ecx
	push	eax

	mov	edi, [ebp + 8]
	mov	eax, [ebp + 12]
	mov	ecx, [ebp + 16]
	rep	stosb

	pop	eax
	pop	ecx
	pop	edi
	mov	esp, ebp
	pop	ebp
	ret
